package com.mer.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mer.dao.PermissionDao;
import com.mer.dao.RolePermissionDao;
import com.mer.entity.Permission;
import com.mer.entity.RolePermission;
import com.mer.entity.User;
import com.mer.service.PermissionService;
import com.mer.utils.WebUtil;
import com.mer.vo.CommonResult;
import com.mer.vo.PermissionVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: IIPhase-project
 * @description:
 * @author: 冯彦凯
 * @create: 2021-10-21 15:55
 **/
@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    private PermissionDao permissionDao;

   /* @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;*/
    @Resource
    private RolePermissionDao rolePermissionDao;
    @Override
    public CommonResult findAllByPage(Integer page, Integer limit, PermissionVo permissionVo) {
        Page<Permission> page1 = new Page(page, limit);
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        List<Permission> permissions = permissionDao.selectList(wrapper);
        permissions.forEach(permission -> {
            //为权限设置身份备注
            if (permission.getType()==1 && permission.getPid().equals("1"))
                permission.setTypeName("主目录");
            else if (permission.getType()==1)
                permission.setTypeName("菜单");
            else
                permission.setTypeName("按钮");
        });
        if (StringUtils.isNotEmpty(permissionVo.getName())){
            wrapper.like("name",permissionVo.getName());
            wrapper.eq("type",1);
            wrapper.ne("pid",1);
            Page<Permission> permissionPage = permissionDao.selectPage(page1,wrapper);
            permissionPage.getRecords().forEach(permission -> {
                if (permission.getType()==1 && permission.getPid().equals("1"))
                    permission.setTypeName("主目录");
                else if (permission.getType()==1)
                    permission.setTypeName("菜单");
                else
                    permission.setTypeName("按钮");
                //递归设置子菜单
                permission.setChildren(getChildren(permission,permissions));
            });
            return new CommonResult(2000,"成功",permissionPage);
        }else {
            wrapper.eq("pid",1);
            Page<Permission> permissionPage = permissionDao.selectPage(page1, wrapper);
            permissionPage.getRecords().forEach(permission -> {
                permission.setTypeName("主目录");
                permission.setChildren(getChildren(permission,permissions));
            });
            return new CommonResult(2000,"成功",permissionPage);
        }
    }

    @Override
    @Transactional
    public CommonResult insert(Permission permission) {
        permissionDao.insert(permission);
        return new CommonResult(2000,"新增成功",permission);
    }

    @Override
    @Transactional
    public CommonResult update(Permission permission) {
        int i = permissionDao.updateById(permission);
        return new CommonResult(2000,"修改成功",permission);
    }

    @Override
    @Transactional
    public CommonResult deleteById(PermissionVo permissionVo) {
        for (String permissionId : permissionVo.getPermissionIds()) {
            permissionDao.deleteById(permissionId);
            QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
            wrapper.eq("permission_id",permissionId);
            rolePermissionDao.delete(wrapper);
        }
        return new CommonResult(2000,"删除成功",null);
    }

    /*@Override
    public CommonResult meanList() {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        User user = JSON.parseObject(s, User.class);
        List<Permission> allPermissionInfo = permissionDao.getAllPermissionInfo(user.getId());
        List<Permission> list = new ArrayList<>();
        for (Permission permission:allPermissionInfo){
            if (permission.getPid().equals("1")){
                list.add(permission);
            }
        }
        for (Permission permission:list){
            permission.setChildren(getChildList(allPermissionInfo,permission.getId()));
        }

        return new CommonResult(2000,"查询成功",list);
    }
    public List<Permission> getChildList(List<Permission> permissions,String pid){
        List<Permission> permissionList = new ArrayList<>();
        for (Permission permission:permissions){
            if (permission.getPid().equals(pid)){
                permissionList.add(permission);
            }
        }
        for (Permission child:permissionList){
            child.setChildren(getChildList(permissions,child.getId()));
        }
        return permissionList;
    }*/

    private List<Permission> getChildren(Permission chPermission,List<Permission> permissions){
        List<Permission> childrenPermissions = new ArrayList<>();
        permissions.forEach(permission -> {
            if (chPermission.getId().equals(permission.getPid()))
                childrenPermissions.add(permission);
        });
        chPermission.setChildren(childrenPermissions);
        if (childrenPermissions.size()==0){
            return childrenPermissions;
        }
        childrenPermissions.forEach(childrenPermission -> {
            childrenPermission.setChildren(getChildren(childrenPermission,permissions));
        });
        return childrenPermissions;
    }
}
